package com.qf.controller.servlet;

import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/19 21:35
 * description:
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet{
    CartService cartService=new CartServiceImpl();
    //清空购物车
    public void clearCartAjax (HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        cartService.clearCartByUid(user.getId());
        response.getWriter().print("success");
    }

    //购物车数量 +/—
    public void addCartAjax(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        //根据goodsid和uid获得购物车信息
        Cart cart=cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        //根据商品id，查询商品对象
        GoodsService goodsService=new GoodsServiceImpl();
        Goods goods=goodsService.findGoodsById(Integer.parseInt(goodsId));
        if(cart!=null){
           if(Integer.parseInt(number)==0){
               cartService.deleteCart(user.getId(),Integer.parseInt(goodsId));
           }else {
               cart.setNum(cart.getNum()+Integer.parseInt(number));
               cart.setMoney(goods.getPrice()*cart.getNum());
               cartService.updateCart(cart);
           }
        }
        response.getWriter().print("success");

    }
    //查询购物车
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        List<Cart> carts=cartService.findCartByUid(user.getId());
        request.setAttribute("carts",carts);
        return "forward:/cart.jsp";


    }
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        User user = (User)request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/login.jsp";
        }
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StringUtils.isEmpty(goodsId)||StringUtils.isEmpty(number)){
            request.setAttribute("msg","添加购物车失败");
            return "forward:/message.jsp";
        }
        //根据商品id，查询商品对象
        GoodsService goodsService=new GoodsServiceImpl();
        Goods goods=goodsService.findGoodsById(Integer.parseInt(goodsId));
        //查询用户有没有添加此商品到购物车，如果此商品已经存在购物车，更新；如果不存在，新增
        Cart cart=cartService.findByUidAndGid(user.getId(),Integer.parseInt(goodsId));
        if(cart==null){//购物车不存在，新增
            cart=new Cart(user.getId(),Integer.parseInt(goodsId),Integer.parseInt(number),(Integer.parseInt(number))*(goods.getPrice()));
            cartService.insert(cart);
        }else{//购物车存在，修改
            cart.setNum(cart.getNum()+Integer.parseInt(number));
            cart.setMoney(goods.getPrice()*cart.getNum());
            cartService.updateCart(cart);
        }
        return "redirect:/cartSuccess.jsp";

    }
}
